<?php

namespace App\Http\Controllers\Company;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\RoomModel;

class RoomController extends Controller
{
    public function list(Request $request){
        $rooms = RoomModel::where('company_id', $request->company_id)->get();
        return self::sendOk($rooms);
    }

    public function add(Request $request){
        $room = RoomModel::create($request->all());
        return self::sendOk($room);
    }

    public function edit(Request $request){
        $room = RoomModel::find($request->id);
        if(!$room){
            return self::sendError('找不到该教室');
        }
        if($room->company_id != $request->company_id){
            return self::sendError('该教室不属于该公司');
        }

        $room->update($request->all());
        return self::sendOk($room);
    }

    public function delete(Request $request){
        $room = RoomModel::find($request->id);
        if(!$room || $room->company_id != $request->company_id){
            return self::sendError('找不到该教室');
        }
        $room->delete();
        return self::sendOk();
    }
}
